<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *作品
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'所有作品'),
	array('op'=>'pass','title'=>'审核通过作品'),
	array('op'=>'unpass','title'=>'审核不通过作品'),
	array('op'=>'edit','title'=>'添加作品'),	
);
$return['status']=$this->status;
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$mid=pdo_get('jy_xiuvote_user',$data);
		$params['mid']=$mid['mid'];
		$return['params']['mid']=$mid['mid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','rid','title','likes','look','money','red_num','thumb','description','status','opus_images','createtime');
	$list=pdo_getslice('jy_xiuvote_opus',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$reply=pdo_get('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['rid']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['username']=$member['username'];
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			$item['sex']=$member['sex']?'男':'女';
			$item['avatar']=$user['avatar'];
			$item['rtitle']=$reply['title'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['opus_images']=json_decode($item['opus_images'],true)[0];
			$item['url']=custom_url('jy_xiuvote/o_detail//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'作品ID','type'=>'string'),
			array('type'=>'img_string','title'=>'参赛作品',
			'data'=>array(
				array('title'=>'作品名称：','name'=>'title'),
				array('title'=>'所属活动：','name'=>'rtitle')
				)
			),
			array('type'=>'img_string','title'=>'发布人','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'username'),
				array('title'=>'昵称：','name'=>'nickname','unit'=>'sex')
				)
			),
			array('name'=>'description','title'=>'作品描述','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'上传时间','type'=>'string'),
		),
		'action' => 
		    array (
			       array('type'=>'post','op'=>'ok','title'=>'审核','message'=>'确定通过该作品吗！'),
			       array('type'=>'post','op'=>'no','title'=>'忽略','message'=>'确定忽略该作品吗！'),
				   ),),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'id','width'=>15,'title'=>'作品ID','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'上传人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'作品标题','type'=>'string'),
		)
	);
	//$return['relation']=array('0'=>'');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='pass'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=1;
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like'=>'%'.$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','rid','title','likes','look','money','red_num','thumb','description','status','opus_images','createtime','basevotenums','baselikenums');
	$list=pdo_getslice('jy_xiuvote_opus',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['title']=cfc('tools')->substr($item['title'],0,28);
			$user=pdo_get('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$reply=pdo_get('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['rid']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['username']=$member['username'];
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			$item['sex']=$member['sex']?'男':'女';
			$item['rtitle']=$reply['title'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['opus_images']=json_decode($item['opus_images'],true)[0];
			$item['votenums']=pdo_count('jy_xiuvote_votelog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'is_shenhe'=>1));
			$jifen=pdo_getsum('jy_xiuvote_sendlplog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'jifen');
			$item['votenums']=$item['basevotenums']+$item['votenums']+$jifen;
			$item['likes']=$item['baselikenums']+$item['likes']+$jifen;
			$item['money']=price($item['money']);
			$item['lpfee']=pdo_getsum('jy_xiuvote_sendlplog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'fee');
			$item['lpfee']=price($item['lpfee']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'作品ID','type'=>'string'),
			array('type'=>'img_string','title'=>'参赛作品',
			'data'=>array(
				array('title'=>'作品名称：','name'=>'title'),
				array('title'=>'所属活动：','name'=>'rtitle')
				)
			),
			array('type'=>'img_string','title'=>'发布人','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'username'),
				array('title'=>'昵称：','name'=>'nickname','unit'=>'sex')
				)
			),
			array('name'=>'opus_images','title'=>'作品图集','type'=>'image'),
			array('type'=>'img_string','title'=>'点赞/投票',
			'data'=>array(
				array('title'=>'点赞数：','name'=>'likes'),
				array('title'=>'投票数：','name'=>'votenums')
				)
			),
			array('type'=>'img_string','title'=>'打赏总额',
			'data'=>array(
				array('title'=>'红包额：','name'=>'money'),
				array('title'=>'礼品额：','name'=>'lpfee')
				)
			),
			array('name'=>'look','title'=>'浏览数','type'=>'string'),
			array('name'=>'createtime','title'=>'上传时间','type'=>'string'),
		),
		'action' => 
		    array (
			     //  array('type'=>'post','op'=>'ok','title'=>'审核','message'=>'确定通过该作品吗！'),
			     // array('type'=>'post','op'=>'no','title'=>'忽略','message'=>'确定忽略该作品吗！'),
				   ),),
		'search'=>array(
		array('name'=>'id','width'=>15,'title'=>'作品ID','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'上传人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'作品标题','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='unpass'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=2;
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like'=>'%'.$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_xiuvote_opus',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$reply=pdo_get('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['rid']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['username']=$member['username'];
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			$item['sex']=$member['sex']?'男':'女';
			$item['avatar']=$user['avatar'];
			$item['rtitle']=$reply['title'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['opus_images']=json_decode($item['opus_images'],true)[0];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'作品ID','type'=>'string'),
			array('type'=>'img_string','title'=>'参赛作品',
			'data'=>array(
				array('title'=>'作品名称：','name'=>'title'),
				array('title'=>'所属活动：','name'=>'rtitle')
				)
			),
			array('type'=>'img_string','title'=>'发布人','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'username'),
				array('title'=>'昵称：','name'=>'nickname','unit'=>'sex')
				)
			),
			array('name'=>'opus_images','title'=>'作品图集','type'=>'image'),
			array('name'=>'description','title'=>'作品描述','type'=>'string'),
			array('name'=>'createtime','title'=>'上传时间','type'=>'string'),
		),
		'action' => 
		    array (
			    //   array('type'=>'post','op'=>'ok','title'=>'审核','message'=>'确定通过该作品吗！'),
			     //  array('type'=>'post','op'=>'no','title'=>'忽略','message'=>'确定忽略该作品吗！'),
				   ),),
		'search'=>array(
		array('name'=>'id','width'=>15,'title'=>'作品ID','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'上传人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'作品标题','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_xiuvote_opus',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$reply=pdo_get('jy_xiuvote_reply',array('id'=>$item['rid'],'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	
	$item['fileddata']=$item['fileddata']?json_decode($item['fileddata'],true):json_decode($reply['fileds'],true);
	$formtypes=get_form_types();
	foreach($item['fileddata'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	$return['item']=$item;
	$return['item']['opus_images']=json_decode($return['item']['opus_images'],true);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回作品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'作品名称','type'=>'string'),
					array('name'=>'thumb','title'=>'作品封面','type'=>'image','help'=>'留空会自动截取视频第一张图片/图集第一张'),
					array('name'=>'rid','title'=>'所属活动','type'=>'select','options'=>'rid'),
					array('type'=>'choose','title'=>'选择发布人','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
					array('name'=>'opus_images','title'=>'作品图集','type'=>'images'),
					array('name'=>'video','title'=>'短视频','type'=>'video','help'=>'支持抖音视频，微信短视频；当活动支持视频展示时才显示！'),
					array('type'=>'choose','title'=>'带货商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
					array('name'=>'description','title'=>'项目介绍','type'=>'text'),
					array('name'=>'basevotenums','title'=>'投票虚数','type'=>'string','help'=>'总票数=虚数+真实数'),
					array('name'=>'baselikenums','title'=>'点赞虚数','type'=>'string','help'=>'总票数=虚数+真实数'),
					array('name'=>'pingfen','title'=>'专家评分','type'=>'unitstring','unit'=>'分'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	if($id){
		$htmls['edits'][]=array(
					'title'=>'表单详情',
					'list'=>array(
							array(
									'tip'=>'@下面是已产生的表单数据，不会因为活动修改了自定义字段而变化！',
									'type'=>'array_look',
									'name'=>'fileddata',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'表单标题'),	
										array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
									),
							)
					)
		);
		}
	$return['rid']=pdo_getall('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['rid']){
		exi('请选择所属活动！','error');
	}
	if(!$postdata['mid']){
		exi('请选择发布人！','error');
	}
	//exi($postdata['lev_name'],'error');exit;
	if(empty($postdata['thumb'])){
		$postdata['thumb']=pdo_getcolumn('core_attachment_first_image',array('video'=>trim($postdata['video']),'uniacid'=>$_SESSION['uniacid']),'image');
	}
	
	$data=array(
		'title'=>trim($postdata['title']),
		'thumb'=>trim($postdata['thumb']),
		'rid'=>intval($postdata['rid']),
		'mid'=>intval($postdata['mid']),
		'opus_images'=>ijson_encode($postdata['opus_images']),
		'description'=>trim($postdata['description']),
		'status'=>intval($postdata['status']),
		'video'=>trim($postdata['video']),
		'goodsid'=>intval($postdata['goodsid']),
		'baselikenums'=>intval($postdata['baselikenums']),
		'basevotenums'=>intval($postdata['basevotenums']),
		'pingfen'=>intval($postdata['pingfen']),
		'isshow'=>1,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_xiuvote_opus',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_xiuvote_opus',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_xiuvote_opus',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='no'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要忽略的作品！','error');
	}
	pdo_update('jy_xiuvote_opus',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已忽略该作品！');
}
if($_RQ['op']=='ok'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要审核的作品！','error');
	}
	pdo_update('jy_xiuvote_opus',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已审核通过该作品！');
}
if($_RQ['op']=='export'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['status']=intval($_RQ['status']);
	$return['params']['status']=$_RQ['status'];
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$mid=pdo_get('jy_xiuvote_user',$data);
		$params['mid']=$mid['mid'];
		$return['params']['mid']=$mid['mid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','rid','title','likes','look','money','red_num','thumb','description','status','opus_images','createtime','basevotenums','baselikenums','fileddata');
	$list=pdo_getall('jy_xiuvote_opus',$params,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['title']=cfc('tools')->substr($item['title'],0,28);
			$user=pdo_get('jy_xiuvote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$reply=pdo_get('jy_xiuvote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['rid']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['username']=$member['username'];
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			$item['sex']=$member['sex']?'男':'女';
			$item['rtitle']=$reply['title'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['opus_images']=json_decode($item['opus_images'],true)[0];
			$item['votenums']=pdo_count('jy_xiuvote_votelog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'is_shenhe'=>1));
			$jifen=pdo_getsum('jy_xiuvote_sendlplog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'jifen');
			$item['votenums']=$item['basevotenums']+$item['votenums']+$jifen;
			$item['likes']=$item['baselikenums']+$item['likes']+$jifen;
			$item['money']=price($item['money']);
			$item['lpfee']=pdo_getsum('jy_xiuvote_sendlplog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'fee');
			$item['lpfee']=price($item['lpfee']);
			$item['fileddata']=json_decode($item['fileddata'],true);
			$item['data2']='';
			if(is_array($item['fileddata'])){
				foreach($item['fileddata'] as $dk=>&$diyv){
						$item['data2'] .='【'.$diyv['title']."】：";
						if(is_array($diyv['data']) && $diyv['type']!=7){
							$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
						}
						if($diyv['type']==7){
							$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
						}
						$item['data2'] .=$diyv['data'];
				}
			}
		}
	}
	$header=array(
		'id'=>'作品ID',
		'title'=>'作品名称',
		'rtitle'=>'所属活动',
		'username'=>'账号',
		'nickname'=>'昵称',
		'likes'=>'点赞数',
		'votenums'=>'投票数',
		'money'=>'红包额',
		'lpfee'=>'礼品额',
		'sex'=>'性别',
		'look'=>'浏览数',
		'data2'=>'表单内容',
		'createtime'=>'发布时间',
	);
	cfc('tools')->export2excel($header,$list,'投票作品');
}